

from util.selenium.webdriver.common.by import By

from base.BaseMethod import BM
from util.DataQuery.FormDataDecorator import FormDataDecorator
from util.sys_dict import legal_type, risk_type


class BaseWriteEquity():
    def click_add_btn(self, driver=None, element_loc = None):
        BM.click(driver=driver, ele_loc=element_loc)
    def select_type(self, driver=None, element_loc = None, type=''):
        BM.select_value_choice(selector_loc=element_loc, text_value=type, driver=driver)
    def input_name(self, driver=None, element_loc = None, name=''):
        BM.input(driver=driver, ele_loc=element_loc, keys=name)
    def input_id_num(self, driver=None, element_loc = None, id_num=''):
        BM.input(driver=driver, ele_loc=element_loc, keys=id_num)
    def input_money(self, driver=None, element_loc = None, money=''):
        money = str(money) if type(money) == float else money
        BM.input(driver=driver, ele_loc=element_loc, keys=money)

# 个人股东信息填写。
class WriteCivilType(BaseWriteEquity):
    num = 1
    @property
    def class_name(self):
        return self.__class__.__name__

    def __init__(self, driver=None):
        self.driver = driver
        if not self.driver:
            raise NameError('driver is not defind. custom--')
    def reset_ele(self):
        main_str = '#personTypeTbodyId tr:nth-of-type({}) '.format(self.num + 1)
        # ######## 公民类型 civil_type
        self.add_btn_loc = (By.CSS_SELECTOR, '#personTypeTbodyId tr.grayTr td:nth-of-type(1) img')
        self.validate_selection_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(2) select')
        self.name_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(3) input')
        self.id_num_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(4) input')
        self.money_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(5) input')
    @FormDataDecorator
    def write_civil_type(self, type='', name='', id_num='', invest=''):
        self.reset_ele()
        self.click_add_btn(driver=self.driver, element_loc=self.add_btn_loc)
        self.select_type(driver=self.driver, element_loc=self.validate_selection_loc,type=type)
        self.input_name(driver=self.driver, element_loc=self.name_loc, name=name)
        self.input_id_num(driver=self.driver, element_loc=self.id_num_loc, id_num=id_num)
        self.input_money(driver=self.driver, element_loc=self.money_loc, money=invest)
        self.num += 1

    @FormDataDecorator
    def write(self, all_list:list=[]):
        a = []
        for item in all_list:
            temp = {"type": "", "name": "", "id_num": "", "invest": ""}
            temp["type"] = str(legal_type.get(item.get("type")))
            temp["name"] = str(item.get("name"))
            temp["id_num"] = str(item.get("id_num"))
            temp["invest"] = str(item.get("invest"))
            a.append(item)
        return a


# 法人股东信息填写
class WriteLegalType(BaseWriteEquity):
    num = 1
    @property
    def class_name(self):
        return self.__class__.__name__

    def __init__(self, driver=None):
        self.driver = driver
        if not self.driver:
            raise NameError('driver is not defind. custom--')

    def reset_ele(self):
        main_str = 'tbody#companyTypeTbodyId tr:nth-of-type({}) '.format(self.num + 1)
        ######## 法人类型 legal_type
        self.add_btn_loc = (By.CSS_SELECTOR, 'tbody#companyTypeTbodyId tr.grayTr td:nth-of-type(1) img')
        self.validate_selection_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(2) select')
        self.name_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(3) input')
        self.id_num_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(4) input')
        self.money_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(5) input')

    @FormDataDecorator
    def write_legal_type(self, type='', name='', code='', invest=''):
        self.reset_ele()
        self.click_add_btn(driver=self.driver, element_loc=self.add_btn_loc)
        self.select_type(driver=self.driver, element_loc=self.validate_selection_loc,type=type)
        self.input_name(driver=self.driver, element_loc=self.name_loc, name=name)
        self.input_id_num(driver=self.driver, element_loc=self.id_num_loc, id_num=code)
        self.input_money(driver=self.driver, element_loc=self.money_loc, money=invest)
        self.num += 1

    @FormDataDecorator
    def write(self, all_list:list=[]):
        a = []
        for item in all_list:
            temp = {"type": "", "name": "", "id_num": "", "invest": ""}
            temp["type"] = str(legal_type.get(item.get("type")))
            temp["name"] = str(item.get("name"))
            temp["id_num"] = str(item.get("id_num"))
            temp["invest"] = str(item.get("invest"))
            a.append(item)
        return a


# 风险投资法人信息填写
class WriteVentureInvestment(BaseWriteEquity):
    num = 1
    @property
    def class_name(self):
        return self.__class__.__name__

    def __init__(self, driver=None):
        self.driver = driver
        if not self.driver:
            raise NameError('driver is not defind. custom--')

    def reset_ele(self):
        main_str = '#moneyCompanyTypeTbodyId tr:nth-of-type({}) '.format(self.num + 1)
        ######## 法人类型（风险投资） venture_investment
        self.add_btn_loc = (By.CSS_SELECTOR, '#moneyCompanyTypeTbodyId tr.grayTr td:nth-of-type(1) img')
        self.validate_selection_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(2) select')
        self.name_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(3) input')
        self.id_num_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(4) input')
        self.money_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(5) input')

    @FormDataDecorator
    def write_venture_investment(self, type='', name='', code='', invest=''):
        self.reset_ele()
        self.click_add_btn(driver=self.driver, element_loc=self.add_btn_loc)
        self.select_type(driver=self.driver, element_loc=self.validate_selection_loc,type=type)
        self.input_name(driver=self.driver, element_loc=self.name_loc, name=name)
        self.input_id_num(driver=self.driver, element_loc=self.id_num_loc, id_num=code)
        self.input_money(driver=self.driver, element_loc=self.money_loc, money=invest)
        self.num += 1

    @FormDataDecorator
    def write(self, all_list:list=[]):
        a = []
        for item in all_list:
            temp = {"type": "", "name": "", "id_num": "", "invest": ""}
            temp["type"] = str(risk_type.get(item.get("type")))
            temp["name"] = str(item.get("name"))
            temp["id_num"] = str(item.get("id_num"))
            temp["invest"] = str(item.get("invest"))
            a.append(item)
        return a


def test(func):
    def wrapper(*args, **kwargs):

        for i in enumerate(range(5), 1):
            func(*args)

    return wrapper
class WriteCivilType_test(BaseWriteEquity):
    num = 1
    @property
    def class_name(self):
        return self.__class__.__name__

    def __init__(self, driver=None):
        self.driver = driver
        if not self.driver:
            raise NameError('driver is not defind. custom--')

    def reset_ele(self):
        main_str = '#personTypeTbodyId tr:nth-of-type({}) '.format(self.num + 1)
        print(main_str)
        # ######## 公民类型 civil_type
        self.add_btn_loc = (By.CSS_SELECTOR, '#personTypeTbodyId tr.grayTr td:nth-of-type(1) img')
        self.validate_selection_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(2) select')
        self.name_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(3) input')
        self.id_num_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(4) input')
        self.money_loc = (By.CSS_SELECTOR, main_str + 'td:nth-of-type(5) input')

    @test
    def write_civil_type(self, type='中国公民', name='叶挺', id_num='123456789101112', invest='10000'):
        self.reset_ele()
        self.num += 1

if __name__ == '__main__':
    # a = WriteCivilType(driver='wi')
    # na = a.__class__.__name__
    # print(f'{na} -- {CLASSMODELMAPPING.get(na)}')
    a = WriteCivilType_test(driver='wi')
    a.write_civil_type()
